<?php
session_start();
define('IZ_ACCNAME','LOCAL');
if(strpos($filename, 'ajax.php') !== false) {
  $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-17);
} elseif(strpos($filename, 'functions_mini.php') !== false) {
  $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-27);
} elseif(strpos($filename, 'functions.php') !== false) {
  $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-22);
} elseif(strpos($filename, 'mini.php') !== false) {
  $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-8);
} else {//if(strpos($filename, 'm.php') !== false) {
  $_SESSION['IZ_CALPATH'] = substr($_SERVER['SCRIPT_FILENAME'],0,strlen($_SERVER['SCRIPT_FILENAME'])-5);
}
if(is_file($_SESSION['IZ_CALPATH'].'includes/data/settings.php')) {
  include_once($_SESSION['IZ_CALPATH'].'includes/vars.php');
  include_once($_SESSION['IZ_CALPATH'].'includes/lang/'.$_SESSION['IZ_LANG'].'_mobile.php');
  include_once($_SESSION['IZ_CALPATH'].'includes/classes.php');
  include_once($_SESSION['IZ_CALPATH'].'includes/functions_mobile.php');
  $user = new user();
  echo '<?xml version="1.0"?><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN"
	"http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link rel="stylesheet" type="text/css" href="',$_SESSION['IZ_CALURL'],'includes/mobile.css"/>';
  
  if($_SESSION['canview'] == 1) {
    if(!empty($_GET['date'])) {
      $date = explode("-",$_GET['date']);
      if(count($date) == 2) { //2 elements (yyyy-mm)
        $daynum = 1;
        $monthnum = $date[1];
        $yearnum = $date[0];
        $secs = mktime(0,0,0,$monthnum,$daynum,$yearnum);
        $ptitle = $_SESSION['IZ_CALNAME'].' - '.$monthnames[date("n",$secs)].' '.date("Y",$secs);
        $view = 'month';
      } elseif(count($date) == 3) { //3 elements (yyyy-mm-dd)
        $daynum = $date[2];
        $monthnum = $date[1];
        $yearnum = $date[0];
        $secs = mktime(0,0,0,$monthnum,$daynum,$yearnum);
        $ptitle = $_SESSION['IZ_CALNAME'].' - '.date("j",$secs).' '.$monthnames[date("n",$secs)].date("Y",$secs);
        $view = 'day';
      }
    } elseif(!empty($_GET['event'])) {
      $event = getdetails($_GET['event']);
      $view = 'event';
      $ptitle = $_SESSION['IZ_CALNAME'].' - '.STR_VIEWINGEVENT;
    } elseif(!empty($_GET['y']) && !empty($_GET['m'])) {
      $yearnum = $_GET['y'];
      $monthnum = $_GET['m'];
      if(empty($_GET['d'])) {
        $daynum = date("j");
        $view = 'month';
      } else {
        $daynum = $_GET['d'];
        $view = 'day';
      }
      $secs = mktime(0,0,0,$monthnum,$daynum,$yearnum);
    } else {
      $secs = mktime();
      $view = 'day';
      $ptitle = $_SESSION['IZ_CALNAME'].' - '.STR_TODAY;
      $mainpage = true;
    }
    if(!empty($_GET['view'])) {
      $view = $_GET['view'];
    }
  } else {
    $ptitle = $_SESSION['IZ_CALNAME'].' - '.STR_LOGIN;
  }
//echo $daynum.':'.$monthnum.':'.$yearnum.':'.count($date);
?>
	<title><?php echo $ptitle; ?></title>
</head>
<body>
	  <div id="header"><?php 
	                    getlink($view.'prev',$secs);
	                    if($view == 'event') { 
	                      getlink('month',$event->eventstart); 
	                    } elseif($view != 'month') {
	                      getlink('month',$secs);
	                    }
	                    getlink($view.'next',$secs);
	                  ?></div>
    
	<div class="content">
	  <?php if($_SESSION['canview'] == 1) {
      switch($_GET['act']) {
        case 'go':
          getnavmenu($daynum,$monthnum,$yearnum);
          $done = true;
          break;
        case 'help': ?>
          <h1><?php echo STR_HELP; ?></h1>
          <h2><?php echo MOB_ACCESSKEYS; ?></h2>
          <p><?php echo MOB_ACCESSKEYSDESC; ?></p>
          <strong>1</strong> - <?php echo MOB_ACCESSPREV; ?><br />
          <strong>2</strong> - <?php echo MOB_ACCESSMONTH; ?><br /> 
          <strong>3</strong> - <?php echo MOB_ACCESSNEXT; ?><br />
          <strong>4</strong> - <?php echo MOB_GOTODAY; ?><br />
          <br />
          <strong>0</strong> - <?php echo MOB_ACCESSHELP; ?><br />
          <strong>5</strong> - <?php echo MOB_ACCESSTODAY; ?><br />
          <br />
          <h2><?php echo MOB_ABOUTIZEIT; ?></h2>
          <p><?php echo sprintf(MOB_ABOUTTEXT,'<a href="'.$_SESSION['IZ_CALURL'].'">'.$_SESSION['IZ_CALURL'].'</a>'); ?></p>
          <?php
          $done = true;
          break;
      }
      if(!$done) {
        switch($view) {
          case 'today':
            get_day(mktime(0,0,0,$curMonth,$curDay,$curYear));
            break;
          case 'day':
            echo get_day($secs);
            break;
          case 'month':
            echo get_month($monthnum,$yearnum);
            break;
          case 'event':
            echo showdetails($event);
            break;
        }
      }
    ?>
    
	</div>
    <?php 
      } else {
        echo '<p>'.ERR_NOPERMISSION.'</p>';
      }
    ?>
  <div id="footer">
    <a accesskey="4" href="<?php echo $_SESSION['IZ_CALURL']; if($_SESSION['IZ_REWRITE'] == 'apache')  { echo 'm/go/'; } else { echo 'm.php?act=go'; } ?>"><?php echo STR_GOTODAY; ?></a> &#124;
    <a accesskey="5" href="<?php echo $_SESSION['IZ_CALURL']; if($_SESSION['IZ_REWRITE'] == 'apache')  { echo 'm/'; } else { echo 'm.php'; } ?>"><?php echo STR_TODAY; ?></a> &#124;
    <a accesskey="0" href="<?php echo $_SESSION['IZ_CALURL']; if($_SESSION['IZ_REWRITE'] == 'apache')  { echo 'm/help/'; } else { echo 'm.php?act=help'; } ?>"><?php echo STR_HELP; ?></a><br />
    <small><a href="http://www.izeit.nu">iZeit <?php echo IZ_CALVERSION; ?></a></small>
  </div>
</body>
</html>
<?php
} else {
  echo '<p>iZeit has not yet been installed.</p>';
}
//var_dump($_GET);
?>